Skip to content

Report actual IP socket address lengths - #666

Merged
nunoplopes merged 1 commit into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-74yt1bdb56
Nov 20, 2026
Merged

nunoplopes merged 1 commit into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-74yt1bdb56

Conversation

@perfloop-agent

@perfloop-agent perfloop-agent commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On Linux, successful refcount IP socket queries report the storage container's size instead of the address length. This gives callers an incorrect length even with a full-sized output buffer.

Derive the length from the concrete IPv4 or IPv6 libc value already being encoded. The shared writeback reports 16 or 28 bytes for getsockname, getpeername and recvfrom, independently of caller capacity. Address bytes and variable Unix lengths are unchanged. Unsupported address families now panic instead of silently reporting a length. The shim length change has no effect on macOS, because nix already reads sa_len there.

When no sender address is available, address outputs remain unspecified. At the base, a NULL source address aborted translated programs (on Linux for IP and named Unix senders; on macOS also for unnamed Unix senders). It now returns normally and leaves an unused length untouched.

This builds on #659: its native-sized serialization buffers and bounded IP copies in rules/socket/shim.rs are preserved.

Testing

Linux Release and Debug socket lit runs each pass 3 tests. Compile-only lit runs of the 2 new fixtures pass in both builds, and both generated refcount programs pass execution in each build. Native/unsafe validation of the two new fixtures remains for upstream CI. macOS is covered by the upstream run-tests.yml matrix.

Clang-format, Rust formatting and clippy checks pass.

Test results and fixture coverage

The executed lit tests are tests/unit/socket_ip_name.c, tests/unit/socket_unix_name.c (now checking portable named and unnamed lengths and printing them for native-versus-translated comparison) and tests/unit/socket_transparent_union.c.

tests/unit/socket_ip_addrlen.c and tests/unit/socket_recvfrom_name.c fail during native execution before the translated programs run. Their complete generated refcount programs are executed separately.

tests/unit/socket_ip_addrlen.c covers IPv4 lengths, IPv6 getsockname length, zero-capacity IPv4 name queries, IPv4 peer and datagram-source bytes, and NULL source addresses with both NULL and non-NULL lengths. tests/unit/socket_recvfrom_name.c covers named and unnamed Unix senders, including NULL source addresses with both NULL and non-NULL lengths. Both generated expectations are included. The existing tests/unit/socket_ip_name.c exercises #659's short-buffer copies alongside the existing Unix and transparent-union socket tests.

To ask for a change, comment or review in plain words.

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart.

Comment thread rules/socket/tgt_refcount.rs Outdated
}
None => {
if !a4.is_null() {
a5.write(0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this store doesn't seem necessary according to the spec

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The zero store is unnecessary under POSIX when no source address is provided, so it is removed along with the fixture's length/family assertion for an unnamed sender. Named-source length checks are unchanged.

Comment thread rules/socket/shim.rs Outdated
_ => {}
}
out_len.write(ss.len());
_ => ss.len(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we panic here rather than accepting silently?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsupported address families now panic instead of reporting a length without encoding an address. The supported IP and Unix branches are unchanged by this revision.

Comment thread rules/socket/shim.rs
});
}
}
ss.len()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should return len here, I believe

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The local len is the truncated copy count, not the address length. POSIX says, “The address_len argument points to a socklen_t object which on input specifies the length of the supplied sockaddr structure, and on output specifies the length of the socket address.” The native C checks in tests/unit/socket_unix_name.c pass with zero and short buffers while requiring the full returned length, so ss.len() is retained.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then it should be something like ::std::mem::size_of_val(&l) as u32. I think ss.len() doesn't make sense

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Linux, native and translated return 19 for the named socket and 2 for the unnamed one; size_of_val(&l) gives 8 because l is a reference, while size_of::<sockaddr_un>() gives 110. POSIX says, “The address_len argument points to a socklen_t object which on input specifies the length of the supplied sockaddr structure, and on output specifies the length of the socket address.” The updated tests/unit/socket_unix_name.c checks portable named and unnamed lengths and prints them for native-versus-translated comparison; do you still want size_of_val?

@perfloop
perfloop Bot force-pushed the perfloop-pr-open-74yt1bdb56 branch from 9040656 to fe9b526 Compare November 20, 2026 12:51
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
@perfloop
perfloop Bot force-pushed the perfloop-pr-open-74yt1bdb56 branch from fe9b526 to f8f307d Compare November 20, 2026 16:28
@nunoplopes
nunoplopes merged commit 67b0ea9 into Cpp2Rust:master Nov 20, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants